/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.core.biz.manager.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.grayrabbit.pay.core.biz.manager.system.TradeTerminalManager;
import org.grayrabbit.pay.core.model.entity.system.TradeTerminal;
import org.grayrabbit.pay.core.model.pages.PageList;
import org.grayrabbit.pay.core.model.results.GenericsResult;
import org.grayrabbit.pay.core.model.results.PayResult;
import org.grayrabbit.pay.core.service.system.AssAppsystemTradeterminalService;
import org.grayrabbit.pay.core.service.system.TradeTerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 
 * 
 * @author liyd
 * @version $Id: TradeTerminalManagerImpl.java, v 0.1 Sat Jun 02 22:00:25 CST
 *          2012 liyd Exp $
 */
@Component
public class TradeTerminalManagerImpl implements TradeTerminalManager {

    @Autowired
    private TradeTerminalService             tradeTerminalService;

    @Autowired
    private AssAppsystemTradeterminalService assAppSystemTradeTerminalService;

    /**
     * 查询业务列表
     * 
     * @param district
     * @return
     */
    public GenericsResult<PageList<TradeTerminal>> queryPageList(TradeTerminal tradeTerminal) {

        GenericsResult<PageList<TradeTerminal>> result = new GenericsResult<PageList<TradeTerminal>>(
            true);

        result.setResultData(tradeTerminalService.queryPageList(tradeTerminal));

        return result;
    }

    /**
     * 根据Id得到实体对象
     * 
     * @param id
     * @return
     */
    public GenericsResult<TradeTerminal> getById(Long id) {

        GenericsResult<TradeTerminal> result = new GenericsResult<TradeTerminal>(true);

        result.setResultData(tradeTerminalService.getById(id));

        return result;

    }

    /**
     * 根据id批量删除
     * 
     * @param idArray
     * @return
     */
    public PayResult deleteBatchById(Long[] idArray) {

        PayResult result = new PayResult(true);
        List<Long> trueList = new ArrayList<Long>();

        StringBuffer names = new StringBuffer(",");
        for (Long id : idArray) {
            if (assAppSystemTradeTerminalService.findByTerminalId(id).size() > 0) {
                names.append(tradeTerminalService.getById(id).getTerminalName());
                names.append(",");
            } else {
                trueList.add(id);
            }
        }
        Long[] trueArray = new Long[trueList.size()];
        trueArray = trueList.toArray(trueArray);
        if (trueArray.length > 0)
            tradeTerminalService.deleteBatchById(trueArray);

        if (trueArray.length < idArray.length) {
            result.setSuccess(false);
            result.setResultMsg("终端:" + names.substring(1) + "有外键约束，不能删除！");
        }

        return result;
    }

    /**
     * 查询所有对象
     * 
     * @return
     */
    public GenericsResult<List<TradeTerminal>> findAll() {

        GenericsResult<List<TradeTerminal>> result = new GenericsResult<List<TradeTerminal>>(true);

        result.setResultData(tradeTerminalService.findAll());

        return result;
    }

    public GenericsResult<List<TradeTerminal>> findEnabledTerminals() {

        GenericsResult<List<TradeTerminal>> result = new GenericsResult<List<TradeTerminal>>(true);

        result.setResultData(tradeTerminalService.findEnabledTerminals());

        return result;
    }

    public GenericsResult<Map<String, TradeTerminal>> findAllMap() {

        GenericsResult<Map<String, TradeTerminal>> result = new GenericsResult<Map<String, TradeTerminal>>(
            true);
        List<TradeTerminal> terminalList = tradeTerminalService.findAll();
        Map<String, TradeTerminal> terminalMap = new HashMap<String, TradeTerminal>();
        for (TradeTerminal terminal : terminalList) {
            terminalMap.put(terminal.getTerminalCode(), terminal);
        }
        result.setResultData(terminalMap);

        return result;
    }

    public GenericsResult<Map<Long, TradeTerminal>> findIdMap() {

        GenericsResult<Map<Long, TradeTerminal>> result = new GenericsResult<Map<Long, TradeTerminal>>(
            true);
        List<TradeTerminal> terminalList = tradeTerminalService.findEnabledTerminals();
        Map<Long, TradeTerminal> terminalMap = new HashMap<Long, TradeTerminal>();
        for (TradeTerminal terminal : terminalList) {
            terminalMap.put(terminal.getTerminalId(), terminal);
        }
        result.setResultData(terminalMap);

        return result;
    }

    /**
     * 添加一个对象
     * 
     * @param tradeTerminal
     * @return
     */
    public GenericsResult<TradeTerminal> insert(TradeTerminal tradeTerminal) {

        GenericsResult<TradeTerminal> result = new GenericsResult<TradeTerminal>(true);
        int num = tradeTerminalService.getCountByTerminalCode(tradeTerminal.getTerminalCode());
        if (num > 0) {
            result.setSuccess(false);
            result.setResultMsg("终端code重复！");
        } else
            tradeTerminalService.insert(tradeTerminal);

        return result;

    }

    public PayResult getIfRepeatByCode(TradeTerminal tradeTerminal) {

        PayResult result = new PayResult(true);
        int num = tradeTerminalService.getCountByTerminalCode(tradeTerminal.getTerminalCode());
        if (num > 0) {
            result.setSuccess(false);
            result.setResultMsg("终端code重复了！");
        }
        return result;

    }

    public GenericsResult<Integer> saveStatus(TradeTerminal tradeTerminal) {

        GenericsResult<Integer> result = new GenericsResult<Integer>(true);

        result.setResultData(tradeTerminalService.changeStatus(tradeTerminal));

        return result;
    }

    /**
     * 更新一个对象
     * 
     * @param tradeTerminal
     * @return
     */
    public GenericsResult<Integer> update(TradeTerminal tradeTerminal) {

        GenericsResult<Integer> result = new GenericsResult<Integer>(true);
        result.setResultData(tradeTerminalService.update(tradeTerminal));

        return result;
    }

    /**
     * 删除一个对象
     * 
     * @param tradeTerminal
     * @return
     */
    public PayResult delete(TradeTerminal tradeTerminal) {

        PayResult result = new PayResult(true);

        tradeTerminalService.delete(tradeTerminal);

        return result;
    }

}
